<template>
  <el-dialog title="外协订单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单名称" prop="orderName">
        <el-input
          v-model="queryParams.orderName"
          placeholder="请输入订单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="orderList"  @current-change="handleCurrent"  @row-dblclick="handleRowDbClick">
      <el-table-column width="55" align="center" >
        <template v-slot="scope">
          <el-radio v-model="selectedOrderCode" :label="scope.row.orderCode" @change="handleRowChange(scope.row)">{{""}}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="订单名称" align="center" prop="orderName" />
      <el-table-column label="生产商编码" align="center" prop="manufactureCode"/>
      <el-table-column label="生产商名称" align="center" prop="manufactureName"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Template from "@/views/print/printtemplate/list.vue";
import BomSelectSingle from "@/components/mdBomSelect/single.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
import {getOrder, listOrder,listLine} from "@/api/mes/pro/outsourcingorder";
import {selectOrderForFeedback} from "@/api/mes/pro/outsourcefeedback";

export default {
  name: "OrderSelector",
  components: {Template, BomSelectSingle, ItemSelect},
  dicts: ['erp_payment_type'],
  props: {
    workorderCode: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      showFlag: false,
      selectedRows: {orderLineList: []},
      oldRow: {},
      orderLineList: [],
      selectedOrderCode: null,
      autoGenFlag: false,
      optType: 'view',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 外协订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: this.workorderCode, orderName: null, status: 1,
        outsourceType: 'G'
      },

    };
  },
  created() {
    this.getList();
  },
  watch: {
    showFlag: {
      handler: function (val) {
        if (val) {
          this.resetForm("queryForm");
          this.selectedRows = {};
          this.getList();
        }
      },
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 查询外协订单列表 */
    getList() {
      this.loading = true;
      selectOrderForFeedback(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.selectedRows = this.orderList.find(obj => {
          return obj.orderCode == this.workorderCode
        })
        this.selectedOrderCode = this.selectedRows ? this.selectedRows.orderCode : null;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedOrderCode = row.orderCode;
        this.selectedRows = row;
        this.$emit('onSelected', this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedOrderCode == null || this.selectedOrderCode == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    },
  }
}
</script>
